//
//  SDCycleScrollerView.m
//  CycleScrollerView
//
//  Created by shansander on 2016/11/25.
//  Copyright © 2016年 shansander. All rights reserved.
//

#import "SDCycleScrollerView.h"

#import <Masonry.h>

@implementation SDCycleScrollerView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    NSLog(@"%@", NSStringFromCGRect(self.bounds));
    [self configView];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configView];
    }
    return self;
}

-(void)configView
{
    _viewmap = [[NSMutableArray alloc] init];
    
    theRootScroolerView = [[UIScrollView alloc] init];
    theRootScroolerView.delegate = self;
    theRootScroolerView.pagingEnabled = true;
    theRootScroolerView.contentMode = UIViewContentModeCenter;
    [self addSubview:theRootScroolerView];
    theRootScroolerView.showsVerticalScrollIndicator = false;
    theRootScroolerView.showsHorizontalScrollIndicator = false;
    [theRootScroolerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset = 0;
        make.left.offset = 0;
        make.right.offset = 0;
        make.bottom.offset = 0;
    }];
    self.sDirction = Scroll_Horizontal;
}

- (void)setSDirction:(Scroll_dirction)sDirction
{
    _sDirction = sDirction;
    if (_sDirction == Scroll_Vertical) {
        theRootScroolerView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height * 3);
    }else{
        theRootScroolerView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
    }
}

- (void)reloadData
{
    if (self.delegate) {
        if (_current_page == 0) {
            for (NSInteger i = 0; i < 3; i ++) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    SDCycleView *view = [self.delegate cycleView:self cycleViewForRow:i];
                    [self addSubview:view withIndex:i];
                });
              
            }
            
        }else{
            for (NSInteger i = 0; i < 3; i ++) {
                if (i < 2) {
                    SDCycleView * view = [self getviewIndex:i + 1];
                    if (view) {
                        [self addSubview:view withIndex:i];
                    }
                }else{
                    SDCycleView * view = [self.delegate cycleView:self cycleViewForRow:_current_page + 1];
                    [self addSubview:view withIndex:i];
                }
            }
        }
        
    }
}

- (BOOL)showNextView
{
    NSLog(@"%s",__func__);
    if (self.current_page == 0) {
        // 这个不需要做什么事情。
    }else{
        NSInteger nex_page = self.current_page + 2;
        SDCycleView * view = [self.delegate cycleView:self cycleViewForRow:nex_page];
        if (view != NULL) {
            [self addLastSubView:view];
            [self displayScrollView];
            return true;
        }else{
            return false;
        }
    }
    return YES;
}
- (BOOL)showPrevsionView{
    NSLog(@"%s",__func__);
    if (self.current_page < 2) {
        
    }else{
        NSInteger prevsion_page = self.current_page - 2;
        SDCycleView * view = [self.delegate cycleView:self cycleViewForRow:prevsion_page];
        if (view != NULL) {
            [self addFirstSubView:view];
            [self displayScrollView];
            return true;
        }
        
    }
    return true;
}
- (SDCycleView *)getviewIndex:(NSInteger)index
{
    return self.viewmap[index];
}

- (void)setCurrent_page:(NSInteger)current_page
{
    BOOL isCanChange = true;
    // 界面发生了改变
    if (_current_page > current_page) {
        isCanChange = [self showPrevsionView];
    }else if (_current_page < current_page){
       isCanChange =  [self showNextView];
    }else{
        // 界面不没有改变
    }
    if (isCanChange) {
        _current_page = current_page;
    }
}
- (void)addSubview:(UIView *)view withIndex:(NSInteger)index
{
    
    [self.viewmap insertObject:view atIndex:index];
    [theRootScroolerView addSubview:view];
    if (_sDirction == Scroll_Vertical) {
        view.frame = CGRectMake(0, index * self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
    }else{
        view.frame = CGRectMake(index * self.bounds.size.width, 0, self.bounds.size.width , self.bounds.size.height);
    }
}

- (void)addFirstSubView:(UIView *)view{
    if (self.viewmap.count == 3) {
        UIView * one = [self.viewmap lastObject];
        [self.viewmap removeLastObject];
        [one removeFromSuperview];
        one = nil;
    }
    [self addSubview:view withIndex:0];
}

- (void)addLastSubView:(UIView *)view
{
    
   
    if (self.viewmap.count == 3) {
        //
        UIView * one = [self.viewmap firstObject];
        [self.viewmap removeObjectAtIndex:0];
        [one removeFromSuperview];
        one = nil;
    
    }
    
    [self addSubview:view withIndex:2];
    
    
}

- (void)displayScrollView
{
    for (int i = 0; i < self.viewmap.count; i ++) {
        UIView * one = self.viewmap[i];
        if (_sDirction == Scroll_Vertical) {
            one.frame = CGRectMake(0, i * self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
        }else{
            one.frame = CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width , self.bounds.size.height);
        }
    }
    
    if (self.current_page > 0) {
        if (_sDirction == Scroll_Vertical) {
            [theRootScroolerView setContentOffset:CGPointMake(0, self.bounds.size.height)];
        }else{
            [theRootScroolerView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
        }
    }
}

- (void)scrollViewDidEnd:(UIScrollView *)scrollView
{
    CGPoint offset_point = scrollView.contentOffset;
    NSInteger page;
    if (self.sDirction == Scroll_Vertical) {
        page = offset_point.y / self.bounds.size.height;
    }else{
        page = offset_point.x / self.bounds.size.width;
    }
    if (self.current_page == 0) {
        if (page > 0) {
            self.current_page += 1;
        }
    }else{
        if (page == 0) {
            self.current_page -= 1;
        }else if (page == 2){
            self.current_page += 1;
        }
    }
    
    NSLog(@"current_page = %ld",self.current_page);

}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEnd:scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
    [self scrollViewDidEnd:scrollView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
